package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BdmGood;
import com.ruoyi.system.domain.BdmMemberRule;
import com.ruoyi.system.domain.BdmUserCollect;
import com.ruoyi.system.domain.dto.BdmGoodDto;
import com.ruoyi.system.domain.dto.UserCollectBto;
import com.ruoyi.system.domain.dto.UserPraiseDto;
import com.ruoyi.system.domain.vo.applet.AppletBdmGoodVo;
import com.ruoyi.system.domain.vo.applet.AppletUserCollectVo;
import com.ruoyi.system.mapper.BdmGoodMapper;
import com.ruoyi.system.mapper.BdmUserCollectMapper;
import com.ruoyi.system.service.IBdmMemberRuleService;
import com.ruoyi.system.service.IBdmUserCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 收藏Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
@Service
public class BdmUserCollectServiceImpl implements IBdmUserCollectService 
{
    @Autowired
    private BdmUserCollectMapper bdmUserCollectMapper;
    @Autowired
    private BdmGoodMapper bdmGoodMapper;

    @Autowired
    private IBdmMemberRuleService bdmMemberRuleService;

    /**
     * 查询收藏
     * 
     * @param id 收藏主键
     * @return 收藏
     */
    @Override
    public BdmUserCollect selectBdmUserCollectById(Long id)
    {
        return bdmUserCollectMapper.selectBdmUserCollectById(id);
    }

    /**
     * 查询收藏列表
     * 
     * @param bdmUserCollect 收藏
     * @return 收藏
     */
    @Override
    public List<BdmUserCollect> selectBdmUserCollectList(BdmUserCollect bdmUserCollect)
    {
        return bdmUserCollectMapper.selectBdmUserCollectList(bdmUserCollect);
    }

    /**
     * 新增收藏
     * 
     * @param userCollectBto 收藏
     * @return 结果
     */
    @Override
    public int insertBdmUserCollect(UserCollectBto userCollectBto)
    {
        BdmUserCollect bdmUserCollect = BeanUtil.copyProperties(userCollectBto, BdmUserCollect.class);
        bdmUserCollect.setCreateTime(DateUtils.getNowDate());
        return bdmUserCollectMapper.insertBdmUserCollect(bdmUserCollect);
    }

    /**
     * 修改收藏
     * 
     * @param bdmUserCollect 收藏
     * @return 结果
     */
    @Override
    public int updateBdmUserCollect(BdmUserCollect bdmUserCollect)
    {
        bdmUserCollect.setUpdateTime(DateUtils.getNowDate());
        return bdmUserCollectMapper.updateBdmUserCollect(bdmUserCollect);
    }

    /**
     * 批量删除收藏
     * 
     * @param ids 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteBdmUserCollectByIds(Long[] ids)
    {
        return bdmUserCollectMapper.deleteBdmUserCollectByIds(ids);
    }

    /**
     * 删除收藏信息
     * 
     * @param id 收藏主键
     * @return 结果
     */
    @Override
    public int deleteBdmUserCollectById(Long id)
    {
        return bdmUserCollectMapper.deleteBdmUserCollectById(id);
    }


    /**
     * 新增/删除收藏
     * @param userCollectBto
     * @return
     */
    @Override
    public int saveCollect(UserCollectBto userCollectBto) {
        userCollectBto.setSysUid(SecurityUtils.getUserId());
        BdmUserCollect collect = BeanUtil.copyProperties(userCollectBto, BdmUserCollect.class);
        List<BdmUserCollect> collectList = selectBdmUserCollectList(collect);
        if (StringUtils.isEmpty(collectList)) {
            collect.setCreateTime(DateUtils.getNowDate());
            return bdmUserCollectMapper.insertBdmUserCollect(collect);
        }else {
            return bdmUserCollectMapper.deleteBdmUserCollectById(collectList.get(0).getId());
        }
    }


    /**
     * 查询收藏列表
     *
     * @param userCollectBto 收藏
     * @return 收藏
     */
    @Override
    public List<AppletUserCollectVo> appletBdmUserCollectList(UserCollectBto userCollectBto)
    {
        Long sysUid = SecurityUtils.getUserId();
        List<AppletUserCollectVo> voList = new ArrayList<>();
        BdmUserCollect collect = new BdmUserCollect();
        collect.setSysUid(sysUid);
        List<BdmUserCollect> list = bdmUserCollectMapper.selectBdmUserCollectList(collect);
        if (StringUtils.isNotEmpty(list)) {
            Long[] goodIdList = list.stream().map(BdmUserCollect::getItemId).toArray(Long[]::new);
            List<AppletBdmGoodVo> goodList = bdmGoodMapper.listGoodVo(BdmGoodDto.builder().goodIds(goodIdList).userId(sysUid).build());
            Map<Long, AppletBdmGoodVo> map = new HashMap<>();
            if (StringUtils.isNotEmpty(goodList)) {
                map = goodList.stream().collect(Collectors.toMap(AppletBdmGoodVo::getId, good -> good));
            }
            Map<Long, AppletBdmGoodVo> finalMap = map;
            list.forEach(c -> {
                AppletUserCollectVo vo = BeanUtil.copyProperties(c, AppletUserCollectVo.class);
                if (StringUtils.isNotEmpty(finalMap)) {
                    AppletBdmGoodVo bdmGood = finalMap.get(c.getItemId());
                    vo.setGoodImg(bdmGood.getGoodImg());
                    vo.setGoodName(bdmGood.getGoodName());
                    vo.setOriginalPrice(bdmGood.getOriginalPrice());
                    vo.setDiscountPrice(bdmGood.getDiscountPrice()); // 会员折扣价
                    vo.setGoodSpecs(bdmGood.getGoodSpecs());
                    UserPraiseDto dto = new UserPraiseDto();
                    dto.setSysUid(vo.getSysUid());
                    dto.setItemId(bdmGood.getId());
                    vo.setIsCollect(isCollect(dto));
                    vo.setVirtualSales(bdmGood.getVirtualSales());
                    vo.setRetailPrice(bdmGood.getRetailPrice());
                }
                voList.add(vo);
            });

        }
        return voList;
    }

    /**
     * 是否收藏 （1收藏，2无）
     * @param userPraiseDto
     * @return
     */
    @Override
    public Long isCollect(UserPraiseDto userPraiseDto) {
        BdmUserCollect collect = new BdmUserCollect();
        collect.setSysUid(userPraiseDto.getSysUid());
        collect.setItemId(userPraiseDto.getItemId());
        List<BdmUserCollect> collectList = bdmUserCollectMapper.selectBdmUserCollectList(collect);
        if (StringUtils.isNotEmpty(collectList)) {
            return 1L;
        }
        return 2L;
    }

    /**
     * 获取当前用户是什么会员
     * @return
     */
    public BdmGood memberLevel(){
        BdmGood bdmGood = new BdmGood();
        Long sysUid = SecurityUtils.getUserId();
        // 查询当前角色等级
        BdmMemberRule bdmMemberRule = new BdmMemberRule();
        bdmMemberRule.setSysUserid(sysUid);
        List<BdmMemberRule> memberRuleList = bdmMemberRuleService.selectBdmMemberRuleList(bdmMemberRule);
        if (StringUtils.isNotEmpty(memberRuleList)) {
            // 是会员
            BdmMemberRule memberRule = memberRuleList.get(0);
            String level = String.valueOf(StringUtils.isNotNull(memberRule.getMemberAgencyLevel()) ? memberRule.getMemberAgencyLevel() : memberRule.getMemberLevel());
            switch (level){
                case "1": // vip
                    bdmGood.setMemberBuyingRights("2");
                    break;
                case "2": // svip
                    bdmGood.setMemberBuyingRights("3");
                    break;
                case "3": // 共享店主
                    bdmGood.setMemberBuyingRights("4");
                    break;
                case "4": // 联创
                    bdmGood.setMemberBuyingRights("5");
                    break;
                case "5": // 区域运营中心
                    bdmGood.setMemberBuyingRights("6");
                    break;
                case "6": // 总裁
                    bdmGood.setMemberBuyingRights("7");
                    break;
                default:
                    bdmGood.setMemberBuyingRights("1"); // 普通
                    break;
            }
        }else {
            // 普通用户
            bdmGood.setMemberBuyingRights("1"); // 普通
        }
        return bdmGood;
    }

    /**
     * 商品价格
     * @return
     */
    public BigDecimal goodPrice(BdmGood bdmGood, String level){
        BigDecimal bigDecimal;
        switch (level){
            case "2": // vip
                bigDecimal = bdmGood.getVipPrice();
                break;
            case "3": // svip
                bigDecimal = bdmGood.getSvipPrice();
                break;
            case "4": // 共享店主
                bigDecimal = bdmGood.getSharePrice();
                break;
            case "5": // 联创
                bigDecimal = bdmGood.getJoinPrice();
                break;
            case "6": // 区域运营中心
                bigDecimal = bdmGood.getOperationsPrice();
                break;
            case "7": // 总裁
                bigDecimal = bdmGood.getPresidentPrice();
                break;
            default: // 售价
                bigDecimal = bdmGood.getRetailPrice();
                break;
        }
        return bigDecimal;
    }
}
